<META http-equiv="refresh" content="<?php echo rand(1,10); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php

/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

$matchId = $_GET["matchId"];
if ($matchId == "") {
	return;
}

//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
$matchDetail = file_get_contents("http://data.sports.sina.com.cn/livecast/log/log_" . $matchId . ".js?" . rand());
$matchDetail = iconv("GBK", "UTF-8", $matchDetail);
$matchDetail = str_replace("show_log(", "", $matchDetail);
$matchDetail = str_replace("}])", "}]", $matchDetail);
$matchDetail = json_decode($matchDetail);

//先清空当前比赛场次的数据
$sql = "delete from live_score where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_score (scoreId, matchId, homeTeamScore, visitTeamScore, scoreTime, scoreAction) VALUES";

$i = 0;
$id;
$match_st;
$match_m;
$match_s1 = null;
$match_s2 = null;
$match_s21 = null;
$match_s22 = null;
foreach ($matchDetail as $match) {
	if (isset ($match->q) && $match->q != "0" && $match->q != "5" && $match->q != "6") {

		if (strpos($match->m, "...") === 0) {
			$match->m = str_replace("...", "", $match->m);
		}
		//不插入第一位是.的内容
		if (strpos($match->m, ".") === 0) {
			continue;
		}
		//不插入包含图片的内容
		if (strpos($match->m, "img") > 0) {
			continue;
		}

		$match_st = "第".$match->q."局";
		$match_m = $match->m;
		if(strpos($match->m, "<font color=blue>")){
			$match_m = substr($match->m, 0, strpos($match->m, "<font color=blue>"));
		}
		if ($match_s21 != $match->s->s21 || $match_s22 != $match->s->s22) {
			
			$match_s = $match->s->d;
			$match_s = explode(" ",$match_s);
			$match_s = $match_s[$match->q - 1];
			$match_s = explode("-",$match_s);
			
			$match_s1 = $match->s->s1 . '(' .$match_s[0] . ')';
			$match_s2 = $match->s->s2 . '(' .$match_s[1] . ')';
			
			$match_s21 = $match->s->s21 ;
			$match_s22 = $match->s->s22;
		} else {
			continue;
		}
		
		$id = $i++;
		$sql .= "('" . $i . "', '" . $matchId . "', '" . $match_s1 . "','" . $match_s2 . "', '" . $match_st . "', '" . $match_m . "'),";

	}
}
echo "<table width='100%'>";
echo "<tr>";
echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_st . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_s1 . " : " . $match_s2 . "</td>";
echo "</tr><tr>";
echo "<td>" . $match_m . "</td>";
echo "</tr>";
echo "</table>";

$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";
//插入比赛比分
mysqli_multi_query($conn, $sql);

/*
 * 暂不采集非直播数据。
 * include_once 'module/simple_html_dom.php';
 * 
if ($matchRecord == "比分板") {
	$matchScoreHTML = file_get_html("http://data.sports.sina.com.cn/live/goal.php?mid=" . $matchId . "&date=" . $matchTime);
	$matchScoreHTML = $matchScoreHTML->find('p');
	$tempMatchInfo = null;
	foreach ($matchScoreHTML as $matchScoreContent) {

		if ($tempMatchInfo != $matchScoreContent->plaintext) {
			$tempMatchInfo = $matchScoreContent->plaintext;
		} else {
			continue;
		}

		$homeTeamScore = false;
		$visitTeamScore = false;

		$scoreTime = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
		$scoreTime = $scoreTime[0];
		if ($matchScoreContent->align == "left") {
			$homeTeamUser = explode(".", $matchScoreContent->plaintext);
			if (count($homeTeamUser) == 1) {
				$homeTeamUser = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
			}
			$homeTeamUser = iconv("gb2312", "UTF-8", $homeTeamUser[1]);
			$visitTeamUser = null;
			$visitTeamScore = true;
		}
		if ($matchScoreContent->align == "right") {
			$visitTeamUser = explode(".", $matchScoreContent->plaintext);
			if (count($visitTeamUser) == 1) {
				$visitTeamUser = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
			}
			$visitTeamUser = iconv("gb2312", "UTF-8", $visitTeamUser[1]);
			$homeTeamUser = null;
			$homeTeamScore = true;
		}
		//红牌
		if (strpos($matchScoreContent->find('img')->src, "bfzb_mj_020.gif")==-1) {
			$scoreAction = "进球";
		}else{
			$scoreAction = "红牌";
		}
		$scoreData[] = array (
			'scoreTime' => $scoreTime,
			'homeTeam' => $homeTeam,
			'visitTeam' => $visitTeam,
			'homeTeamUser' => $homeTeamUser,
			'visitTeamUser' => $visitTeamUser,
			'scoreAction' => $scoreAction,
			'$homeTeamScore' => $homeTeamScore,
			'$visitTeamScore' => $visitTeamScore
		);
	}
}
*/
?>